<?php

/**
 * Maikuro Framework
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * Represents a database result including the row set, the number of affected
 * rows and the last insert id.
 */
class DbResult implements Iterator, Countable
{
    /**
     * The number of affected rows.
     *
     * @var integer
     */
    protected $affectedRows;

    /**
     * The last insert id (if any).
     *
     * @var integer
     */
    protected $lastInsertId;

    /**
     * The row set result (if any).
     * 
     * @var array
     */
    protected $rowSet = array();

    /**
     * Counts the row set/affected rows.
     *
     * @return integer
     */
    public function count()
    {
        return count($this->rowSet) ? count($this->rowSet) : $this->affectedRows;
    }

    /**
     * Returns the current element of the row set.
     *
     * @return StdClass
     */
    public function current()
    {
        return current($this->rowSet);
    }

    /**
     * Returns the number of affected rows.
     *
     * @return integer
     */
    public function getAffectedRows()
    {
        return $this->affectedRows;
    }

    /**
     * Returns the first row from the row set.
     *
     * @return StdObject|null
     */
    public function getFirstRow()
    {
        return isset($this->rowSet[0]) ? $this->rowSet[0] : null;
    }

    /**
     * Returns the last insert id.
     *
     * @return integer
     */
    public function getLastInsertId()
    {
        return $this->lastInsertId;
    }

    /**
     * Returns the row set.
     *
     * @return array
     */
    public function getRowSet()
    {
        return $this->rowSet;
    }

    /**
     * Returns the current key of the row set.
     *
     * @return StdClass
     */
    public function key()
    {
        return key($this->rowSet);
    }

    /**
     * Returns the next item of the row set.
     *
     * @return StdClass
     */
    public function next()
    {
        return next($this->rowSet);
    }

    /**
     * Rewinds the row set.
     *
     * @return mixed
     */
    public function rewind()
    {
        return reset($this->rowSet);
    }

    /**
     * Sets the number of affected rows.
     *
     * @param integer $affectedRows
     * @return DbResult
     */
    public function setAffectedRows($affectedRows)
    {
        $this->affectedRows = (int) $affectedRows;
        return $this;
    }

    /**
     * Sets the last insert id.
     *
     * @param integer $lastInsertId
     * @return DbResult
     */
    public function setLastInsertId($lastInsertId)
    {
        $this->lastInsertId = $lastInsertId;
        return $this;
    }

    /**
     * Sets the row set.
     *
     * @param array $rowSet
     * @return DbResult
     */
    public function setRowSet($rowSet)
    {
        $this->rowSet = $rowSet;
        return $this;
    }

    /**
     * Checks if the current element of the row set is valid.
     *
     * @return boolean
     */
    public function valid()
    {
        return key($this->rowSet) !== null;
    }
}